/**
 * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.afterturn.easypdf.entity;

import lombok.Data;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

/**
 * word导出,图片设置和图片信息
 *
 * @author JueYue
 * 2017-9-15
 * @version 3.0.1
 */
@Data
public class PdfImageEntity {

    public static int EMBED = 0;
    public static int ABOVE = 1;
    public static int BEHIND = 2;
    /**
     * 图片占PDF大小类型,1 是绝对值, 2是 百分比
     */
    public static int PX = 1;
    public static int PERCENT = 2;

    public static String URL = "url";

    public static String Data = "data";
    /**
     * 图片输入方式
     */
    private String type = URL;

    private int startX = 20;

    private int startY = -999;
    /**
     * 图片宽度
     */
    private int width;
    // 图片高度
    private int height;
    // 图片地址
    private String url;
    // 图片信息
    private byte[] data;

    private int rowspan = 1;
    private int colspan = 1;
    /**
     * 图片方式
     * EMBED 围绕
     * ABOVE 浮于上方
     * BEHIND 浮于下方
     */
    private int locationType = PdfImageEntity.EMBED;

    private int widthUnitType = PdfImageEntity.PX;
    private int heightUnitType = PdfImageEntity.PX;

    public PdfImageEntity() {

    }

    public PdfImageEntity(byte[] data, int width, int height) {
        this.data = data;
        this.width = width;
        this.height = height;
        this.type = Data;
    }

    public PdfImageEntity(String url, int width, int height) {
        this.url = url;
        this.width = width;
        this.height = height;
    }


    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight(PDRectangle pageSize) {
        if (heightUnitType == PdfImageEntity.PERCENT) {
            return (int) (pageSize.getHeight() * height / 100);
        }
        return height;
    }

    public int getWidth(PDRectangle pageSize) {
        if (widthUnitType == PdfImageEntity.PERCENT) {
            return (int) ((pageSize.getWidth() - 40) * width / 100);
        }
        return width;
    }
}
